package com.luo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.luo.constant.MessageConstant;
import com.luo.constant.RedisConstant;
import com.luo.entity.PageResult;
import com.luo.entity.QueryPageBean;
import com.luo.entity.Result;
import com.luo.pojo.SetMeal;
import com.luo.service.SetMealService;

import com.luo.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author Zoro
 * @date 2021年08月26
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    /**
     * 分页查询
     */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/pageSelect.do")
    public PageResult setMealPageSelect(@RequestBody QueryPageBean queryPageBean) {

        PageInfo<SetMeal> setMealPageInfo = setmealService.setMealPageSelect(queryPageBean);
        return new PageResult(setMealPageInfo.getTotal(), setMealPageInfo.getList());
    }

    /**
     * 图片文件
     */
    @RequestMapping("/upload.do")
    public Result upload(MultipartFile imgFile) {
        try {
            byte[] imgFileBytes = imgFile.getBytes();
            String filename = imgFile.getOriginalFilename();
            String extensionName = filename.substring(filename.indexOf('.'));
            filename = UUID.randomUUID() + "-" + System.currentTimeMillis() + extensionName;
            QiniuUtils.upload2Qiniu(imgFileBytes, filename);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, filename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
        }
    }

    /**
     * 添加数据
     */
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/setMealAdd.do")
    public Result setMealAdd(Integer[] travelGroupIds, @RequestBody SetMeal setMeal) {
        try {
            setmealService.setMealAdd(travelGroupIds, setMeal);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 根据id查询数据
     */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/getSetMeal.do")
    public Result getSetMealById(Integer setMealId) {
        try {
            SetMeal setMeal = setmealService.getSetMealById(setMealId);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setMeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 编辑数据
     */
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @RequestMapping("/setMealEdit.do")
    public Result setMealEdit(Integer[] travelGroupIds,@RequestBody SetMeal setMeal) {
        try {
            setmealService.setMealEdit(travelGroupIds,setMeal);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_SETMEAL_FATL);
        }
    }


    /**
     * 删除数据
     */
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @RequestMapping("/setMealDelete.do")
    public Result setMealDeleteById(Integer setMealId){
        try {
            setmealService.setMealDeleteById(setMealId);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_SETMEAL_FATL);
        }
    }

}
